/*
 * CameraMgr.java
 *
 * Created on March 21, 2006, 3:17 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

/**
 *
 * @author dmgreen
 */

package Media;

//as a test !
import Comm.Util.Logger;
import Experience.*;
import javax.microedition.lcdui.Form;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Image;

import javax.microedition.media.control.*;
import javax.microedition.media.*;

import java.util.Vector;

import java.io.IOException;

public class CameraMgr {
    
    private Player myPlayer;
    private VideoControl myControl;
    
    boolean PLAYBACK = false;
    
    Canvas myCanv = null;
        
    /** Creates a new instance of CameraMgr */
    public CameraMgr(Canvas camcanv) {
        myCanv = camcanv;
    
         initialize();
    
    }
    
    private void initialize() {
        
        try {
            myPlayer = Manager.createPlayer("capture://video");
            myPlayer.realize();
        }
        
        catch (IOException ioe) {  
           Logger.getLog().addLogMessage("IOException while initializing camera: " + ioe.toString(), Logger.MSG_ERROR); 
           return;
        }
        catch (MediaException me) { 
            Logger.getLog().addLogMessage("MediaException while initializing camera: " + me.toString(), Logger.MSG_ERROR);
           return;
        }
        catch(SecurityException se){
           Logger.getLog().addLogMessage("SecurityException while initializing camera: " + se.toString(), Logger.MSG_ERROR);
           return;
        }

        myControl = (VideoControl) myPlayer.getControl("VideoControl");
        
        myControl.initDisplayMode(
                      myControl.USE_DIRECT_VIDEO, myCanv
                       );

    
    }
    
    public void hideViewfinder() {
        //stop();
        try {
            //Logger.getLog().addLogMessage("Stopping camera ...");
            myPlayer.stop();
            //Logger.getLog().addLogMessage("Deallocating camera ...");
            myPlayer.deallocate();
        }
        catch (MediaException me) {
             Logger.getLog().addLogMessage("MediaException while stopping camera player: " + me.toString(), Logger.MSG_ERROR); 
        }
        
        myControl.setVisible(false);
        
    }
       
    public void showViewfinder(int yPos){
         
        myControl.setDisplayLocation(2, yPos);
            
        try {
            if (myPlayer.getState() != Player.STARTED) {
                //Logger.getLog().addLogMessage("Prefetching camera ...");
                myPlayer.prefetch();
                //Logger.getLog().addLogMessage("Starting camera ...");
                myPlayer.start();
            }
        
            myControl.setVisible(true);
        }
        catch (MediaException me) {
             Logger.getLog().addLogMessage("MediaException while showing viewfinder: " + me.getMessage(), Logger.MSG_ERROR); 
        }
        catch(SecurityException se){
             Logger.getLog().addLogMessage("SecurityException while showing viewfinder: " + se.getMessage(), Logger.MSG_ERROR);
        }  
         catch(Exception e) {
             Logger.getLog().addLogMessage("Unknown Exception while showing viewfinder: " + e.getMessage(), Logger.MSG_ERROR);
        }  
    }
    
     public byte[] takeSnapshot() {
         
        Image image;
        byte[] raw;
        
        try{
            raw = myControl.getSnapshot("encoding=jpeg&width=320&height=240");
        }
        catch (MediaException me){
             Logger.getLog().addLogMessage("MediaException occurred while taking snapshot: " + me.getMessage(), 
                     Logger.MSG_ERROR);
             return null;
        }
        
        return raw;
                
   }
         
}
